#include <boost/mem_fn.hpp>
#include <boost/bind.hpp>

#include <algorithm>
#include "Material.h"

using namespace slak::mesh;

void Material::render(PatchList patch_list)
{
	setState();
	std::for_each(patch_list.begin(), patch_list.end(),
		      boost::mem_fn(&Patch::render)); 
	resetState();
}

void MultipassMaterial::render(PatchList patch_list)
{
	std::for_each(passes.begin(),
		      passes.end(),
		      boost::bind(&Material::render, _1, patch_list));
}

void OglMaterial::setState()
{
	glOnOff(GL_BLEND, blend);
	glPolygonMode(GL_FRONT_AND_BACK, polygon_mode);
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
}

void OglMaterial::resetState()
{
	glDisable(GL_BLEND);
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}


